package org.jmh.gift.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.jmh.gift.dto.PlaceOrderDTO;
import org.jmh.gift.service.IOrderGiftService;
import org.jmh.gift.domain.OrderGift;
import org.jmh.gift.query.OrderGiftQuery;
import org.jmh.gift.util.AjaxResult;
import org.jmh.gift.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import com.baomidou.mybatisplus.plugins.Page;

import javax.validation.Valid;

@RestController
@RequestMapping("/orderGift")
public class OrderGiftController {
    @Autowired
    public IOrderGiftService orderGiftService;


    /**
     * 保存和修改公用的
     * @param orderGift  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderGift orderGift){

            if( orderGift.getId()!=null)
                orderGiftService.updateById(orderGift);
            else
                orderGiftService.insert(orderGift);
            return AjaxResult.success();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
            orderGiftService.deleteById(id);
            return AjaxResult.success();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
            OrderGift orderGift = orderGiftService.selectById(id);
            return AjaxResult.success(orderGift);

    }

    @GetMapping("/parentOrderNo/{parentOrderNo}")
    public AjaxResult getByParentOrderNo(@PathVariable("parentOrderNo")String parentOrderNo){
        List<OrderGift> orderGiftList = orderGiftService.selectList(
                new EntityWrapper<OrderGift>().eq("parent_order_no", parentOrderNo));
        return AjaxResult.success(orderGiftList);
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

            List< OrderGift> list = orderGiftService.selectList(null);
            return AjaxResult.success(list);

    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult json(@RequestBody OrderGiftQuery query)
    {

        Page<OrderGift> page = new Page<OrderGift>(query.getPage(),query.getRows());
        page = orderGiftService.selectPage(page);
        PageList<OrderGift> pageList = new PageList<OrderGift>(page.getTotal(),page.getRecords());
            return AjaxResult.success(pageList);

    }

    @PostMapping("/placeOrder")
    public AjaxResult placeOrder(@Valid @RequestBody PlaceOrderDTO dto){
        orderGiftService.placeOrder(dto);
        return AjaxResult.success(dto.getUniPayOrderSn());
    }


}
